package com.monkey.demolist.greendao.n;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.monkey.demolist.R;

public class OneVNActivity extends AppCompatActivity {

    private static final String TAG = "OneVNActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_vn);

        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "author.db", null);
        SQLiteDatabase db = devOpenHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();

        AuthorDao authorDao = daoSession.getAuthorDao();
        PostDao postDao = daoSession.getPostDao();

        //储存一个作者
        Author author = new Author();
        author.setName("tom");
        author.setSex("boy");
        authorDao.insert(author);

        // 获取插入后的作者id
        Author authorByQuery = authorDao.queryBuilder().where(AuthorDao.Properties.Name.eq("tom"),
                AuthorDao.Properties.Sex.eq("boy")).unique();

        //写入该作者对应的三篇博客
        Post first = new Post();
        first.setAuthorId(authorByQuery.getId());
        first.setContent("第一篇文章");

        Post second = new Post();
        second.setAuthorId(authorByQuery.getId());
        second.setContent("第二篇文章");

        Post third = new Post();
        third.setAuthorId(authorByQuery.getId());
        third.setContent("第三篇文章");
        postDao.insertInTx(first,second,third);

        //查询存储的结果
        Author authorResult = authorDao.queryBuilder().where(AuthorDao.Properties.Name.eq("tom"), AuthorDao.Properties.Sex.eq("boy")).unique();
        Log.e(TAG, "onCreate: "+authorResult.getName());
        Log.e(TAG, "onCreate: "+authorResult.getSex());
        for (int i = 0; i < authorResult.getPosts().size(); i++) {
            Log.e(TAG, "onCreate: "+authorResult.getPosts().get(i).getContent() );
        }


    }
}
